'use strict';

angular.module('Initialize').controller('TerminalBindController', ['$scope', '$state', 'BaseModule', 'BaseEnum', 'BaseFunction', 'BaseService', '$location', 'Authentication',
    function($scope, $state, BaseModule, BaseEnum, BaseFunction, BaseService, $location, Authentication) {
        $scope.btnSave = function(){
            //收集当前作业的绑定记录
            if(oldWLFocusedRecord != null){
                var stationTerminalRecords = $scope.Module.StationTerminal.CacheRecords;
                for(var i=0; i<stationTerminalRecords.length; i++){
                    if(stationTerminalRecords[i].WorkLocation === oldWLFocusedRecord._id){
                        $scope.Module.StationTerminal.removeRecord(stationTerminalRecords[i]);
                        i--;
                    }
                }
                $scope.Module.StationTerminal.Records = $scope.Module.StationTerminal.CacheRecords.concat($scope.Module._StationTerminal.CacheRecords);
            }

            for(var i=0; i<$scope.Module.StationTerminal.Records.length; i++){
                if($scope.Module.StationTerminal.Records[i].Terminal._id == undefined){
                    BaseFunction.alert('数据错误', '终端不能为空');
                    return;
                }
            }
            $scope.Module.StationTerminal.save(function(){
                $scope.Module.StationTerminal.Records = [];
                $scope.Module._StationTerminal.Records = [];
                $scope.Module._StationTerminal.emitOnLoadEvent();
                $scope.Module.WorkLocation.load();
            });
        };
        $scope.btnCancel = function(){
            $state.reload();
        };
        var oldWLFocusedRecord = null;
        $scope.rowChange = function(){
            //收集当前工位的绑定记录
            if(oldWLFocusedRecord != null){
                var stationTerminalRecords = $scope.Module.StationTerminal.CacheRecords;
                for(var i=0; i<stationTerminalRecords.length; i++){
                    if(stationTerminalRecords[i].WorkLocation === oldWLFocusedRecord._id){
                        $scope.Module.StationTerminal.removeRecord(stationTerminalRecords[i]);
                        i--;
                    }
                }
                $scope.Module.StationTerminal.Records = $scope.Module.StationTerminal.CacheRecords.concat($scope.Module._StationTerminal.CacheRecords);
            }

            oldWLFocusedRecord = $scope.Module.WorkLocation.FocusedRecord;
            $scope.Module._StationTerminal.Records = [];
            $scope.Module._StationTerminal.FocusedRecord = null;

            //载入当前工位的绑定记录
            if($scope.Module.WorkLocation.FocusedRecord.IsLoadStationTerminal){
                var _StationTerminalRecords = [];
                var stationTerminalRecords = $scope.Module.StationTerminal.CacheRecords;

                for(var i=0; i<stationTerminalRecords.length; i++){
                    if(stationTerminalRecords[i].WorkLocation === $scope.Module.WorkLocation.FocusedRecord._id){
                        _StationTerminalRecords.push(stationTerminalRecords[i]);
                    }
                }
                $scope.Module._StationTerminal.Records = _StationTerminalRecords;  
                $scope.Module._StationTerminal.emitOnLoadEvent();
            }
            else{
                $scope.Module.StationTerminal.find({WorkLocation: $scope.Module.WorkLocation.FocusedRecord._id}, null, $scope.Module.StationTerminal.Populate, function(records){
                    $scope.Module.WorkLocation.FocusedRecord.IsLoadStationTerminal = true;                    
                    $scope.Module._StationTerminal.Records = records;
                    $scope.Module.StationTerminal.Records = $scope.Module.StationTerminal.CacheRecords.concat(records);
                    $scope.Module._StationTerminal.emitOnLoadEvent();
                });
            }
        };

        $scope.addRow = function(record){
            record.WorkLocation = $scope.Module.WorkLocation.FocusedRecord._id;
        };
        $scope.cellValueChange = function(columnName, newValue, oldValue){
            if(columnName === "Terminal.Code"){
                if(newValue.length > 0){
                    $scope.Module._StationTerminal.FocusedRecord.Terminal.DeviceType = newValue[0].DeviceType;
                    $scope.Module._StationTerminal.FocusedRecord.Terminal.UniqueID = newValue[0].UniqueID;
                }
                else{
                    $scope.Module._StationTerminal.FocusedRecord.Terminal.DeviceType = '';
                    $scope.Module._StationTerminal.FocusedRecord.Terminal.UniqueID = '';
                }
                $scope.Module._StationTerminal.emitRowsDataChangeEvent();
            }
        };

        $scope.refBeforeShow = function(refScope) {
            var terminalIds = [];
            for (var i = 0; i < $scope.Module._StationTerminal.Records.length; i++) {
                if ($scope.Module._StationTerminal.Records[i].Terminal !== undefined && $scope.Module._StationTerminal.Records[i].Terminal !== "") {
                    terminalIds.push($scope.Module._StationTerminal.Records[i].Terminal._id);
                }
            }
            refScope.Module.Condition = {
                _id: {
                    $nin: terminalIds
                },
                IsEffective: true
            };
        };

        BaseModule.initModule($scope, 'WorkLocation');
        $scope.Module.WorkLocation.LoadMode = BaseEnum.LoadMode.CacheAndPaging;
        $scope.Module.WorkLocation.PageSize = 15;
        $scope.Module.WorkLocation.Populate = {path: 'Department', select: 'Name'};

        BaseModule.initModule($scope, 'StationTerminal');
        $scope.Module.StationTerminal.LoadMode = BaseEnum.LoadMode.CacheAndNoPaging;
        $scope.Module.StationTerminal.Populate = {path: 'Terminal', select: 'Code Name DeviceType UniqueID'};
        $scope.Module.StationTerminal.PageSize = 15;

        BaseModule.initModule($scope, '_StationTerminal');
        $scope.Module._StationTerminal.LoadMode = BaseEnum.LoadMode.CacheAndNoPaging;
        $scope.Module._StationTerminal.PageSize = 15;
    }
]);
